package com.dongdongsongcan.app.utils;

import android.content.Context;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.dongdongsongcan.app.DDApp;
import com.dongdongsongcan.app.control.AddressControl;
import com.dongdongsongcan.app.control.bean.DDLocation;
import com.dongdongsongcan.app.control.bean.DDLocationData;
import com.su.dolphin.control.task.CtrlTask;
import com.su.dolphin.utils.LogUtil;

public class LocationUtil {


    private static BDLocation sBDLocation;//缓存百度定位的地址

    private static BDLocation getBDLocation() {
        return sBDLocation;
    }


    public interface OnReceiveLocationListener {
        public void onReceiveLocation(BDLocation bdLocation);
    }

    public static void getLocation(Context context) {
        Context appContext = context.getApplicationContext();
        final LocationClient client = new LocationClient(appContext);
        //client.setAccessKey("eYyy3sq4he0NG8P742Fy04jw");
        client.registerLocationListener(new BDLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation bdLocation) {
                LogUtil.d(bdLocation.getLatitude() + ":latitude");
                LogUtil.d(bdLocation.getLongitude() + ":longtitude");
                LogUtil.d(bdLocation.getProvince() + ":getProvince");
                LogUtil.d("return code:" + bdLocation.getLocType());
                client.stop();
                sBDLocation = bdLocation;
                AddressControl.i().address_nearby(bdLocation.getLatitude(), bdLocation.getLongitude(), new CtrlTask.IControlResponse<DDLocationData>() {
                    @Override
                    public void onControlResponse(DDLocationData data) {
                        DDLocation ddLocation = data.getResult();
                        //ddLocation.setSite_y(sBDLocation.getLatitude());
                        //ddLocation.setSite_x(sBDLocation.getLongitude());
                        DDApp.saveDDLocation(ddLocation);
                    }
                });
            }


        });

        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);
        option.setPriority(LocationClientOption.NetWorkFirst);
        option.setIsNeedAddress(true);
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setCoorType("bd09ll");
        option.setScanSpan(5000);
        client.setLocOption(option);
        client.start();
        client.requestLocation();
    }
}
